﻿using Groove.Core.Player;
using System.Collections.Generic;
using System.IO;

namespace Groove.Core.MediaFile
{
    public class LocalMediaFileFinder
    {
        public static List<IMediaFile> SearchMediaFiles(string folder)
        {
            if (string.IsNullOrEmpty(folder)) return null;
            return GetMediaFromDirectoryRecursionly(folder);
        }

        private static List<IMediaFile> GetMediaFromDirectoryRecursionly(string dirPath)
        {
            var ret = new List<IMediaFile>();
            var dirInfo = new DirectoryInfo(dirPath);
            var fileList = dirInfo.GetFiles();
            foreach (var file in fileList)
            {
                if (!SupportedFileType.IsSupported(file.Extension)) continue;
                ret.Add(new MediaFile(file.FullName));
            }
            var subDiretories = dirInfo.GetDirectories();
            foreach (var dir in subDiretories)
            {
                ret.AddRange(GetMediaFromDirectoryRecursionly(dir.FullName));
            }
            return ret;
        }
    }
}
